package com.protocols.sctp.m3ua;

import com.protocols.GeneralProtocolData;
import com.protocols.OctaveInteger;
import com.protocols.ProtoOperations;
import com.protocols.ProtocolStackException;

public class M3uaBeat  implements GeneralProtocolData{

	private OctaveInteger M3ua_Version= new OctaveInteger(1,1);
	private OctaveInteger Reserved= new OctaveInteger(0,1);
	private OctaveInteger M3ua_Message_class= new OctaveInteger(3,1);	//ASP State MAintenance Message	
	private OctaveInteger M3ua_Message_type= new OctaveInteger(3, 1);// Beat Message
	private OctaveInteger M3ua_MessageLength= new OctaveInteger(4);
	
	public HeartBeatData HeartBeatData = new HeartBeatData(false);
	public M3uaBeat() {
		
	}
	
	public byte [] getByteArray(){		
		M3ua_MessageLength.setValue(8+HeartBeatData.getLength());
		return  ProtoOperations.concatArrays(M3ua_Version.getByteArray(), Reserved.getByteArray(), M3ua_Message_class.getByteArray(), M3ua_Message_type.getByteArray(), M3ua_MessageLength.getByteArray(),HeartBeatData.getByteArray());

	}

	public int getLength(){
		return getByteArray().length;
	}
	
	public int getM3ua_Version() {
		return M3ua_Version.Value;
	}


	public int getM3ua_Message_class() {
		return M3ua_Message_class.Value;
	}



	public int getM3ua_Message_type() {
		return M3ua_Message_type.Value;
	}



	public int getM3ua_MessageLength() {
		return M3ua_MessageLength.Value;
	}

	public byte[] decode(byte [] input) throws ProtocolStackException{
		
		if (input[0]==1 && input[2]==3 && input[3]==3){			
			
			M3ua_MessageLength.Value= ProtoOperations.intFromByteArray(input, 4, 4);
			
			// extract the m3ua body
			byte [] remainder = ProtoOperations.extractByteArray(input, 8,M3ua_MessageLength.Value-8 );
			
			HeartBeatData.decode(remainder);
	
			
		}
		else
			throw new ProtocolStackException("Decode Failure:M3uaASPDownAck"+ProtoOperations.getHexString(input));
		
		
		return null;
	}
	


	
	

	
	

}
